<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>对象使用key或者index的组合来获取属性值</title>
  </head>
  <body>
    <script>
      function paramHandles(obj = {}) {
        function toTypeString(value) {
          return Object.prototype.toString.call(value).slice(8, -1).toLowerCase();
        }

        var params = [];

        function paramHandle(obj = {}, pkey) {
          var type = toTypeString(obj);

          switch (type) {
            case "string":
            case "number":
            case "boolean":
              params.push([pkey, obj]);
              break;

            case "undefined":
            case "null":
              params.push([pkey, ""]);
              break;

            default:
              Object.entries(obj).forEach(function ([key, val]) {
                var nkey = pkey ? `${pkey}[${key}]` : key;
                type = toTypeString(val);

                switch (type) {
                  case "object":
                    paramHandle(val, nkey);
                    break;

                  case "array":
                    val.forEach(function (v, i) {
                      paramHandle(v, `${nkey}[${i}]`);
                    });
                    break;

                  case "undefined":
                  case "null":
                    params.push([nkey, ""]);
                    break;

                  default:
                    // val 是基本数据类型
                    params.push([nkey, val]);
                    break;
                }
              });
              break;
          }
        }

        paramHandle(obj);

        return params;
      }

      const obj = {a:'这是a',b:'这是b',c:[1, {d:'这是数组里的对象'}, {e:{f:'这是数组里对象的对象'}}, 4]};
      const res = paramHandles(obj);
      console.log(res);
    </script>
  </body>
</html>
